﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigTestV3.common;
using BigTestV3.xmlHelp;

namespace BigTestV3
{
    public partial class frmStatus : Form
    {
        public frmStatus()
        {
            InitializeComponent();
        }

        private List<TestStudent> students;
        private DataGridView dataGridView;

        /// <summary>
        /// 加载已经登录成功的学生的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormStatusLoad(object sender, EventArgs e)
        {
            try
            {
                List<TestStudent> tempstudents = new List<TestStudent>();

                string testpath = FilePathTotal.BigTestInfo;

                if (File.Exists(testpath))
                {
                    BigTestInfo papers = XmlHelper.XmlDeserializeFromFile<BigTestInfo>(testpath, Encoding.UTF8);
                    //studentList = papers.TestPlace.List;

                    students = GetStudeents(papers.TestTestPlaceStu);
                    string path = FilePathTotal.LoginSuccessPath;
                    if (File.Exists(path))
                    {
                        LoginSuccessStu loginSuccessStu = XmlHelper.XmlDeserializeFromFile<LoginSuccessStu>(path,
                                                                                                            Encoding.
                                                                                                                UTF8);
                        foreach (string num in loginSuccessStu.StuNumber)
                        {
                            foreach (TestStudent student in students)
                            {
                                if (string.Equals(num, student.StuNum))
                                {
                                    student.IsUpload = false;
                                    tempstudents.Add(student);
                                }
                            }
                        }
                        if (tempstudents.Count > 0)
                        {
                            #region 动态生成

                            //dataGridView = new DataGridView();                       
                            //dataGridView.DataSource = tempstudents;

                            //this.Controls.Add(dataGridView);
                            //dataGridView.Columns[0].HeaderCell.Value = "学号";
                            //dataGridView.Columns[1].HeaderCell.Value = "姓名";
                            //dataGridView.Columns[2].HeaderCell.Value = "是否登录";


                            //dataGridView.Dock=DockStyle.Fill;

                            //for (int i=0; i<dataGridView.ColumnCount;i++)
                            //{
                            //    dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                            //} 

                            #endregion

                            dgv.DataSource = tempstudents;

                        }
                        else
                        {
                            MessageBox.Show("还没有学生登录");
                        }
                    }
                    else
                    {
                        MessageBox.Show("还没有学生登录");
                    }
                }
                timer.Enabled = true;
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        /// <summary>
        /// 获取学生信息 集合
        /// </summary>
        /// <param name="studentList"></param>
        /// <returns></returns>
        private List<TestStudent> GetStudeents(List<XmlTestPlaceStu> studentList)
        {
            List<TestStudent> studentsList = new List<TestStudent>();
            TestStudent testStudent = null;
            foreach (XmlTestPlaceStu s in studentList)
            {
                testStudent = new TestStudent() {StuName = s.Name, StuNum = s.StudentNumber, IsLogin = "否"};
                studentsList.Add(testStudent);
            }

            return studentsList;
        }

        /// <summary>
        /// 判断已经提交答案的学生的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimerTick(object sender, EventArgs e)
        {
            try
            {
                BasicSetting setting = null;
                List<string> dic = new List<string>();
                List<TestStudent> tempstudents = new List<TestStudent>();

                string path = FilePathTotal.SettingPath;

                if (File.Exists(path))
                {
                    setting = XmlHelper.XmlDeserializeFromFile<BasicSetting>(path, Encoding.UTF8);
                }
                if (setting != null)
                {
                    if (!Directory.Exists(setting.TecPath))
                    {
                        Directory.CreateDirectory(setting.TecPath);
                    }
                    else
                    {
                        DirectoryInfo dicInfo = new DirectoryInfo(setting.TecPath);

                        FileInfo[] textFiles = dicInfo.GetFiles("*", SearchOption.TopDirectoryOnly);

                        foreach (FileInfo info in textFiles)
                        {
                            if (info.FullName.Contains(".xml"))
                            {
                                string temp = info.Name.Substring(0, info.Name.IndexOf("."));
                                if (dgv != null && dgv.Rows.Count >= 1)
                                    for (int i = 0; i < students.Count; i++)
                                    {
                                        if (temp == students[i].StuNum)
                                        {
                                            students[i].IsLogin = "是";
                                            students[i].IsUpload = true;
                                            tempstudents.Add(students[i]);
                                        }
                                    }
                            }
                        }
                        if (tempstudents.Count > 0 && dgv != null)
                        {
                            dgv.DataSource = tempstudents;
                            dgv.Refresh();
                        }
                    }
                }
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }


        }
    }
}